package com.rslai.base.tool.servertest.command;

import com.rslai.base.tool.servertest.context.Context;
import com.rslai.base.tool.servertest.exception.ExecuteException;
import com.rslai.base.tool.servertest.response.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;

public class WaitStepCommand extends StepCommand {
    long waitTime;

    public WaitStepCommand(long time) {
        this.waitTime = time;
    }

    public Response doExecute(Response result, Context context) throws Exception {
        try {
            this.logger.info("wait command<time={}ms> is starting...", Long.valueOf(this.waitTime));
            TimeUnit.MILLISECONDS.sleep(this.waitTime);
            return result;
        } catch (InterruptedException e) {
            String message = String.format("wait step command<time=%sms> execute error", new Object[]{Long.valueOf(this.waitTime)});
            this.logger.error(message, e);
            throw new ExecuteException(message, e);
        }
    }

    public StepCommand doClone() {
        return new WaitStepCommand(this.waitTime);
    }

    public Map<String, Object> toReport() {
        Map details = new HashMap();
        details.put("stepName", "等待:");
        details.put("name", String.valueOf(this.waitTime) + "ms");
        List params = new ArrayList();
        details.put("params", params);
        return details;
    }
}